package com.example.personalizededucationsystem.repository;

import com.example.personalizededucationsystem.model.AnswerRecord;
import com.example.personalizededucationsystem.model.Question;
import com.example.personalizededucationsystem.model.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;
import java.util.Optional;

public interface AnswerRecordRepository extends JpaRepository<AnswerRecord, Long> {

    Optional<AnswerRecord> findFirstByStudentAndQuestionOrderByAnsweredAtDesc(User student, Question question);

    @Query("SELECT ar FROM AnswerRecord ar JOIN FETCH ar.question q JOIN FETCH q.knowledgePoints WHERE ar.student = :student")
    List<AnswerRecord> findAllByStudentWithDetails(@Param("student") User student);

    @Query("SELECT ar FROM AnswerRecord ar JOIN ar.student s JOIN ClassMember cm ON cm.student = s WHERE cm.myClass.classId = :classId AND ar.isCorrect = false")
    List<AnswerRecord> findAllIncorrectRecordsByClassId(@Param("classId") Long classId);
}